package com.gxa.modules.property.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.AssertUtils;
import com.gxa.common.utils.Result;
import com.gxa.modules.property.dto.ComplaintDto;
import com.gxa.modules.property.dto.ComplaintToUpdate;
import com.gxa.modules.property.entity.Complaint;
import com.gxa.modules.property.entity.ComplaintType;
import com.gxa.modules.property.service.ComplaintService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@Api(tags = "投诉管理接口")
@Slf4j
public class ComplaintController {
    @Autowired
    private ComplaintService complaintService;

    @ApiOperation(value = "投诉添加")
    @PostMapping("/complaint/add")
    public Result add(@RequestBody ComplaintDto complaintDto){
        if (complaintDto != null){
            this.complaintService.add(complaintDto);
            return new Result().ok();
        }else {
            return new Result().error();
        }

    }

    @ApiOperation(value = "投诉删除")
    @DeleteMapping("/complaint/delete")
    public Result delete(@RequestParam Integer id){
        this.complaintService.delete(id);
        return new Result().ok();
    }

    @ApiOperation(value = "投诉更新")
    @PutMapping("/complaint/update")
    public Result update(@RequestBody ComplaintToUpdate complaintToUpdate){
        if (complaintToUpdate != null){
            this.complaintService.update(complaintToUpdate);
            return new Result().ok();
        }else {
            return new Result().error();
        }
    }

    @ApiOperation(value = "投诉查询(查询条件 处理状态id 投诉人名字)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value = "当前是第几页",dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value = "每页显示多少条",dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "stateId",value = "处理状态id",dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "complainant",value = "投诉人姓名",dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "communityId",value = "小区编号",dataType = "int"),


    })
    @GetMapping("/complaint/list")
    public com.gxa.modules.property.utils.Result<List<Complaint>> queryComplaint(@RequestParam Map<String,Object> params){
        log.info("----params----{}----",params);
        AssertUtils.isBlank(String.valueOf(params),"字符串不能为空");
        com.gxa.modules.property.utils.Result<List<Complaint>> r = new com.gxa.modules.property.utils.Result<>();
        try {
            PageInfo<Complaint> pageInfo = complaintService.queryComplaint(params);
            r = com.gxa.modules.property.utils.Result.success(pageInfo.getList(),pageInfo.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            com.gxa.modules.property.utils.Result.failed();
        }
        return r;
    }

    @ApiOperation("投诉类型下拉列表")
    @GetMapping("/complaintType/queryComplaintTypeList")
    public List<ComplaintType> queryComplaintType(){
        return complaintService.queryComplaintType();
    }
}
